import {defineStore} from "pinia";

export const useAppStore = defineStore('app', {
    persist: true, //使用持久化插件
    state: () => {   //是一个函数，返回一个对象，用于存储状态
        return {
            collapsed: true,
            theme: 'light',
            previewTheme: 'default',
            codeTheme: 'atom',
            searchHistory: [{
                key: '123',
                label: '123'
            }]
        }
    },
    getters: {},  //用于存储计算属性
    actions: {  //用于封装业务逻辑
        pushSearchHistory(val: any) {
            let tmp = {
                key: val,
                label: val,
            }
            const index = this.searchHistory.findIndex(item => item.key === tmp.key && item.label === tmp.label);
            if (index !== -1) {
                this.searchHistory.splice(index, 1);
            }
            this.searchHistory.unshift(tmp);
            if (this.searchHistory.length > 5) {
                this.searchHistory.pop();
            }
        },
        removeSearchHistory() {
            this.searchHistory = []
        },
        setCodeTheme(val: any) {
            this.codeTheme = val
        },
        setPreviewTheme(val: any) {
            this.previewTheme = val
        },
        switchCollapsed() {
            this.collapsed = !this.collapsed
        },
        switchTheme() {
            if (this.theme === 'light') this.theme = 'dark'
            else this.theme = 'light'
        }
    },
})